public class Solution {
    public int lengthOfLIS(int[] nums) {

        int len = nums.length;

        //以i为结尾的最长递增子序列长度
        int[] dp = new int[len];

        //初始化
        dp[0] = 1;

        //填表
        for (int i = 1; i < len; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            if (dp[i] == 0) {
                dp[i] = 1;
            }
        }

        int ret = 0;
        for (int num : dp) {
            ret = Math.max(ret, num);
        }

        return ret;

    }

    public int lengthOfLIS2(int[] nums) {
        int len = nums.length;

        int[] ret = new int[len];
        int index = 0;

        ret[index] = nums[0];

        for (int i = 0; i < len; i++) {
            if (nums[i] > ret[index]) {
                ret[++index] = nums[i];
            } else {
                int left = 0, right = index;
                while (left < right) {
                    int mid = left + (right - left) / 2;
                    if (ret[mid] < nums[i]) {
                        left = mid + 1;
                    }else {
                        right = mid;
                    }
                }
                ret[left] = nums[i];
            }
        }

        return index + 1;
    }
}
